package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.CabinetType;
import com.share.device.service.ICabinetTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YC
 */
@Tag(name = "柜机类型接口管理")
@RestController
@RequestMapping("/cabinetType")
public class CabinetTypeController extends BaseController {

    @Autowired
    private ICabinetTypeService cabinetTypeService;

    /**
     * 根据一个id删一个，根据多个id删多个
     */
    @Operation(summary = "删除柜机类型（一个或多个）")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        boolean addResult = cabinetTypeService.removeBatchByIds(ids);
        return toAjax(addResult);
    }

    @Operation(summary = "修改柜机类型")
    @PutMapping("/modify")
    public AjaxResult modify(@RequestBody CabinetType cabinetType) {
        boolean addResult = cabinetTypeService.updateById(cabinetType);
        return toAjax(addResult);
    }

    @Operation(summary = "新增柜机类型")
    @RequiresPermissions("device:cabinetType:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CabinetType cabinetType) {
        boolean addResult = cabinetTypeService.save(cabinetType);
        return toAjax(addResult);
    }

    @Operation(summary = "查询所有柜机类型集合")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<CabinetType> list = cabinetTypeService.list();
        return success(list);
    }

    @Operation(summary = "根据ID查询柜机类型详情")
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        CabinetType cabinetType = cabinetTypeService.getById(id);
        return success(cabinetType);
    }

    @Operation(summary = "分页查询柜机类型集合")
    @Log(title = "柜机类型分页", businessType = BusinessType.OTHER)
    @RequiresPermissions("device:cabinetType:query")
    @PostMapping("/page")
    public TableDataInfo page(@RequestBody CabinetType cabinetType) {
        // 若依框架封装分页参数
        startPage();
        // 调用service查询数据库
        List<CabinetType> list = cabinetTypeService.queryCabinetTypeList(cabinetType);
        return getDataTable(list);
    }

}
